﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using LMHTX.Modules.DIRECTORY.COOPERATIVE.Components;

namespace LMHTX.Modules.DIRECTORY.COOPERATIVE.Controls
{
    public partial class CooperativeManager : AddEditItemControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                ddlArea.DataSource = Area.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlArea.DataBind();

                ddlCareer.DataSource = Career.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlCareer.DataBind();

                ddlType.DataSource = Components.Type.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlType.DataBind();

                if (Request.QueryString["careerid"] != null)
                {
                    // Hiển thị theo ngành nghề.
                    ddlCareer.SelectedValue = Request.QueryString["careerid"];
                }
                else if (Request.QueryString["areaid"] != null)
                {
                    // Hiển thị theo địa bàn.
                    ddlArea.SelectedValue = Request.QueryString["areaid"];
                }
                else if (Request.QueryString["typeid"] != null)
                {
                    // Hiển thị theo loại hình.
                    ddlType.SelectedValue = Request.QueryString["typeid"];
                }

                this.Search();

            }
        }

        protected void gvCooperative_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCooperative.PageIndex = e.NewPageIndex;
            if (ViewState["CooperativeDataSet"] != null)
            {
                gvCooperative.DataSource = ViewState["CooperativeDataSet"];
                gvCooperative.DataBind();
            }
            else
            {
                this.Search();
            }
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.Search();
        }

        private void Search()
        {
            string value = txtSearchValue.Text.Trim();
            string where = string.Format("CO.Title LIKE '%{0}%' AND CO.AreaID LIKE '{1}' AND CO.CareerID LIKE '{2}' AND CO.TypeID LIKE '{3}'", value, ddlArea.SelectedValue, ddlCareer.SelectedValue, ddlType.SelectedValue);
            ViewState["CooperativeDataSet"] = Cooperative.Search(this.ModuleID, where);
            gvCooperative.DataSource = ViewState["CooperativeDataSet"];
            gvCooperative.PageIndex = 0;
            gvCooperative.SelectedIndex = -1;
            gvCooperative.DataBind();
        }
    }
}